﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;

namespace BuXiaChu.OrderDishes.Web.Admin.City
{
    public partial class AddArea : System.Web.UI.Page
    {
        protected string message = "";
        protected string parentId = "0";
        protected List<AreaModel> areaList = null;
        protected void Page_Load(object sender, EventArgs e)
        {

            InitDropDownList();
            if (Page.IsPostBack)
            {
                #region
                message = "失败";
                if (!string.IsNullOrEmpty(txt_Name.Value))
                {
                    AreaModel area = new AreaModel();
                    area.Name = txt_Name.Value.Trim();
                    area.CityCode = txt_CityCode.Value.Trim();
                    area.Remarks = txt_Remarks.Value.Trim();
                    area.ParentId = int.Parse(sel_ParentId.Value);
                    var id = 0;
                    if (int.TryParse(Request.QueryString["id"], out id))
                    {
                        area.Id = id;
                    }
                    if (AreaManager.Save(area))
                    {
                        message = "ok";
                    }
                }
                #endregion
            }
            else
            {
                var id = 0;
                if (int.TryParse(Request.QueryString["id"], out id))
                {
                    AreaModel area = AreaManager.GetByAreaId(id);
                    if (area != null)
                    {
                        txt_Name.Value = area.Name;
                        txt_CityCode.Value = area.CityCode;
                        txt_Remarks.Value = area.Remarks;
                        sel_ParentId.Value = area.ParentId.ToString();
                    }
                }
            }
        }
        private void InitDropDownList()
        {
            areaList = AreaManager.GetListByParentId(0);
            foreach (var item in areaList)
            {
                var listItem = new ListItem(item.Name, item.Id.ToString());
                sel_ParentId.Items.Add(listItem);
            }
            if (string.IsNullOrEmpty(Request.QueryString["areaId"])==false)
            {
                sel_ParentId.Value = Request.QueryString["areaId"];
            }
        }
    }
}
